#ifndef _COMPLEX_CUBIC_H
#define _COMPLEX_CUBIC_H

#include "icell.h"

#include <vector>

// ===============================================================
//  Déclaration de la classe complexe cubique : ComplexCubic
// ===============================================================

template<int t_iDimComp, int t_iDimPoint, typename t_Type>
class ComplexCubic
{
	public:
		ComplexCubic();
		~ComplexCubic();
		
		void AddICell(VirtualICell* p_picCell);
		bool IsValid();
		std::vector<VirtualICell*>::iterator FindICell(VirtualICell* p_picCell);
		
	private:
		std::vector<std::vector<VirtualICell*>*> m_vComplexes; // std::vector < blablabla > de taille (t_iDimComp + 1)
		
};

// ===============================================================
//  Implantation des fonctions membres.
// ===============================================================

template<int t_iDimComp, int t_iDimPoint, typename t_Type>
ComplexCubic<t_iDimComp, t_iDimPoint, t_Type>::ComplexCubic()
{
	for (int i = 0; i <= t_iDimComp; i++)
		m_vComplexes.push_back(new std::vector<VirtualICell*>());
}

// ---------------------------------------------------------------
template<int t_iDimComp, int t_iDimPoint, typename t_Type>
ComplexCubic<t_iDimComp, t_iDimPoint, t_Type>::~ComplexCubic()
{
	std::vector<VirtualICell*>::iterator it;

	for (int i = 0; i < m_vComplexes.size(); i++)
	{
		std::cout << i << std::endl;
		for (it = m_vComplexes[i]->begin(); it != m_vComplexes[i]->end(); )
		{
			//delete (*it);
			it = m_vComplexes[i]->erase(it);
		}
	}
	
	m_vComplexes.erase(m_vComplexes.begin(), m_vComplexes.end());
}

// ---------------------------------------------------------------
template<int t_iDimComp, int t_iDimPoint, typename t_Type>
void ComplexCubic<t_iDimComp, t_iDimPoint, t_Type>::AddICell(VirtualICell* p_picCell)
{
	if (p_picCell->GetDim() == 0);
		m_vComplexes[0]->push_back(p_picCell);
}

// ---------------------------------------------------------------
template<int t_iDimComp, int t_iDimPoint, typename t_Type>
bool ComplexCubic<t_iDimComp, t_iDimPoint, t_Type>::IsValid()
{
	for (int i = 0; i < m_vComplexes.size(); i++)
	{
		std::vector<VirtualICell*>::iterator it;
		for (it = m_vComplexes[i]->begin(); it != m_vComplexes[i]->end(); ++it)
		{
			std::vector<VirtualICell*> picBorderCells = *(*it)->GetBorderCells();
			for (int j = 0; j < picBorderCells.size(); i++)
			{
				if (picBorderCells[j] == NULL)
					return false;
			}
		}
	}
	return true;
}

// ---------------------------------------------------------------
template<int t_iDimComp, int t_iDimPoint, typename t_Type>
std::vector<VirtualICell*>::iterator ComplexCubic<t_iDimComp, t_iDimPoint, t_Type>::FindICell(VirtualICell* p_picCell)
{
	std::cout << "prout";
	std::cout << m_vComplexes.size();
	assert(!m_vComplexes.empty());
	
	int i = 0;
	for (i = 0; i < m_vComplexes.size(); i++)
	{
		std::vector<VirtualICell*>::iterator it;
		for (it = m_vComplexes[i]->begin(); it != m_vComplexes[i]->end(); ++it)
		{
			if (*it == p_picCell)
				return it;
		}
	}
	return m_vComplexes[i]->end();
}

#endif // _COMPLEX_CUBIC_H
